<html>

<head>

<?php

include "../includes/constants.php";
include "../includes/functions.php";

?>

<title></title>

<script src="../includes/functions.js"></script>

</head>

<body>

<?php

if ($_POST) {
	if (isset($_POST["slot_old"])) {
		$query_post = "UPDATE merchantlist SET slot='" . $_POST["slot_new"] . "', item='" . $_POST["item_new"] . "' WHERE merchantid='" . $_GET["merchantid"] . "' AND slot='" . $_POST["slot_old"] . "'";
	} else {
		$query_post = "INSERT INTO merchantlist (merchantid,slot,item) VALUES ('" . $_GET["merchantid"] . "','" . $_POST["slot_new"] . "','" . $_POST["item_new"] . "')";
	};
	if (!mysql_query($query_post,$db)) {
		echo mysql_error() . "<br>\n";
	};
};

if ($_GET["merchantid"]) {
	$query_get = "SELECT * FROM merchantlist WHERE merchantid='" . $_GET["merchantid"] . "' ORDER BY slot ASC";
	$result_get = mysql_query($query_get,$db);
	
	echo "<table border=\"0\">
	<tr>
		<th colspan=\"2\">Slot</th>
		<th colspan=\"2\">Item</th>
	</tr>\n";
	
	if ($db_row_get = mysql_fetch_array($result_get)) {
		do {
			echo "\t<tr>
		<form method=\"POST\" action=\"\">
		<input type=\"hidden\" name=\"slot_old\" value=\"" . $db_row_get["slot"] . "\">
		<td></td>
		<td><input type=\"text\" size=\"1\" name=\"slot_new\" value=\"" . $db_row_get["slot"] . "\"></td>
		<td><input type=\"text\" size=\"2\" name=\"item_new\" value=\"" . $db_row_get["item"] . "\"></td>
		<td><a href=\"javascript:void\" onclick=\"OpenItem('../items/?id=' + item_new.value);return false\">View</a></td>
		<td><button type=\"submit\">Update</button></td>
		<td>";
		if ($db_row_get["slot"] == $_POST["slot_new"]) {
			echo "<font color=\"blue\">Updated!</font>";
		};
		echo "</td>
		</form>
	</tr>\n";
		} while ($db_row_get = mysql_fetch_array($result_get));
	};
	
	echo "\t<tr>
		<form method=\"POST\" action=\"\">
		<td></td>
		<td><input type=\"text\" size=\"1\" name=\"slot_new\"></td>
		<td><input type=\"text\" size=\"2\" name=\"item_new\"></td>
		<td><a href=\"javascript:void\" onclick=\"OpenItem('../items/?id=' + item_new.value);return false\">View</a></td>
		<td><button type=\"submit\">Update</button></td>
		<td></td>
		</form>
	</tr>\n";
	
	echo "</table>";
	
} else {

?>

<form method="GET" action="">
	Merchant ID: <input type="text" size="3" name="merchantid"><button type="submit">Get</button>
</form>

<?php

};

?>

</body>

</html>